<?php
/**
 * Used to display extra fields in the payment methods found in the admin interface.
 * Admin Menu: Shop Settings -> Payment Methods -> Main.
 * 
 * The extra fields are saved into the DB in the table klarnaconfig.
 * 
 * @author Klarna
 */
class klarna_config extends oxAdminView {
	
	private $kutil = null;
	private $kconfig = null;
	
	/**
	 * Adds extra information that is used in the view.
	 * 
	 * @Override
	 * @return returns Klarnas edited version of the template file.
	 */
	public function render() {
		if ( oxConfig::getParameter("aoc") ) {
			return parent::render();
		}
		parent::render();
		
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}

		$installer = $this->kutil->getInstallObject();
		$this->_aViewData['isDone'] = $installer->isDone();
		
		if(!$installer->isDone())
		{
		    oxUtils::getInstance()->redirect("index.php?stoken=".$_GET['stoken']."&cl=klarna_install");
		}

		$this->_aViewData["kconfig"] = $this->kutil->getKlarnaConfig();
		$this->_aViewData["kutil"] = $this->kutil;
		$this->_aViewData["hasPclasses"] = !klarnapclasses::isEmpty();
		$this->_aViewData["pclasses"] = $this->kutil->getPclasses();
		$temp = $this->kutil->getDefaultCountries();
		$this->_aViewData['displayDefaultCountry'] = count($temp) > 1 ? true : false;
		$oDB = oxDb::getDb(true);
		foreach ($temp as $key => $value) {
			$sql = "SELECT OXTITLE from oxcountry WHERE OXISOALPHA2 = '$key';";
			$temp[$key] = $oDB->getOne($sql);
		}
		$this->_aViewData['thedefaultcountrys'] = $temp;
		$baseurl = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/modules/klarna/images';
		$baseurl = str_replace('/admin', '', $baseurl);
		$this->_aViewData['logo'] = $baseurl . '/klarna.png';
		
		$temp = oxUtils::getInstance()->assignValuesFromText( $this->_aViewData["kconfig"]->klarnaconfig__sellto->value );
		
		$oDB = oxDb::getDb(true);

		foreach ($temp as $oField) {
			$oField->value = $oField->name[0];
			$id = substr($oField->name, 2);
			$sql = "SELECT OXTITLE from oxcountry WHERE OXISOALPHA2 = '$id';";
			$oField->name = $oDB->getOne($sql);
		}
		
		$this->_aViewData["kcountries"] = $temp;
		
		return "klarna_config.tpl";
	}
	
	/**
	 * Saves payment parameters changes.
	 * Extended to also save Klarnas payment options.
	 *
	 * @return mixed
	 */
	public function save() {
		parent::save();
		
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}
		$paymentid = oxConfig::getParameter( "oxid");
		
		$soxId = $this->getConfig()->getShopId();
		$editedKConfigParams = oxConfig::getParameter("kconfedit");
		
		if ( !isset( $editedKConfigParams['klarnaconfig__testmode'])) {
			$editedKConfigParams['klarnaconfig__testmode'] = 0;
		}

		$this->kconfig = $this->kutil->getKlarnaConfig();

		if(!$this->kconfig->klarnaconfig__oxid->rawValue) {
			$this->kconfig->klarnaconfig__oxid = new oxField($soxId, oxField::T_RAW);;
		}
		
		//Contains only the selected countries.
		$countries = oxConfig::getParameter("countries");
		$sellToString = $this->kutil->createSellToString($this->kconfig->klarnaconfig__sellto->value, $countries);
		
		$this->kconfig->klarnaconfig__sellto = new oxField($sellToString, oxField::T_RAW);
		$this->kconfig->assign($editedKConfigParams);
		$this->kconfig->save();
	}
	
	/**
	 * Saves the changes made in the admin view and after that downloads the
	 * pclasses from Klarnas server.
	 */
	public function updatePclasses() {
		$this->save();
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}
		
		$this->kutil->getNewPclasses();
	}
}
